package com.ruoyi.common.utils;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;

import java.util.concurrent.TimeUnit;

/**
 * 项目工具类
 */
public class MyUtil {

    /**
     * 获取单号
     *
     * @return 返回单号
     */
    public static String getBillNumber() {
        String billNumber = "DD-" + DateUtils.dateTime();
        String key = Constants.BILL_KEY + billNumber;
        Object value = SpringUtils.getBean(RedisCache.class).getCacheObject(key);
        int code = 1;
        if (StringUtils.isNull(value)) {
            SpringUtils.getBean(RedisCache.class).setCacheObject(key, 1, 1, TimeUnit.DAYS);
        } else {
            code = Integer.parseInt(value.toString());
            code++;
            SpringUtils.getBean(RedisCache.class).setCacheObject(key, code, 1, TimeUnit.DAYS);
        }
        return String.format(billNumber + "%04d", code);
    }
}
